import os
from dotenv import load_dotenv

# 加载环境变量
load_dotenv()

class Settings:
    # API 配置
    API_HOST: str = os.getenv("API_HOST", "0.0.0.0")
    API_PORT: int = int(os.getenv("API_PORT", "8000"))
    DEBUG: bool = os.getenv("DEBUG", "True").lower() == "true"
    
    # Discourse 配置
    DISCOURSE_BASE_URL: str = os.getenv("DISCOURSE_BASE_URL", "https://www.isharkfly.com/")
    DISCOURSE_API_KEY: str = os.getenv("DISCOURSE_API_KEY", "7f817d54eddc6fe749e974e5ac0cec43d6a5d237a21a7234e7a11d8debe61d17")
    DISCOURSE_USERNAME: str = os.getenv("DISCOURSE_USERNAME", "kiler398")
    
    # MongoDB 配置
    MONGODB_URL: str = os.getenv("MONGODB_URL", "mongodb://158.69.124.134:27017")
    MONGODB_DATABASE: str = os.getenv("MONGODB_DATABASE", "checkee_data")
    MONGODB_USERNAME: str = os.getenv("MONGODB_USERNAME", "mongoadmin")
    MONGODB_PASSWORD: str = os.getenv("MONGODB_PASSWORD", "xHtOTc6uYOQnF*")
    
    # Redis 配置（可选，用于缓存）
    REDIS_URL: str = os.getenv("REDIS_URL", "redis://localhost:6379")

    def get_mongodb_url(self) -> str:
        """构建完整的 MongoDB 连接 URL"""
        if self.MONGODB_USERNAME and self.MONGODB_PASSWORD:
            # 如果有用户名和密码，构建带认证的 URL
            base_url = self.MONGODB_URL.replace("mongodb://", "")
            return f"mongodb://{self.MONGODB_USERNAME}:{self.MONGODB_PASSWORD}@{base_url}"
        else:
            # 如果没有用户名密码，使用原始 URL
            return self.MONGODB_URL

settings = Settings()
